//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;
using System.Workflow.ComponentModel;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Data Reader Http")]
    [Description("Read data from Http server.")]
    [WorkflowCategory("Web")]
    public class HttpDataReader : Activity
    {
        public static DependencyProperty FileNameProperty = DependencyProperty.Register("FileName",
            typeof(Uri), typeof(HttpDataReader));

        [RequiredInputParam]
        [Name("FileName")]
        [Description("URL of the file to be downloaded.")]
        public Uri FileName
        {
            get
            {
                return ((Uri)(base.GetValue(HttpDataReader.FileNameProperty)));
            }
            set
            {
                base.SetValue(HttpDataReader.FileNameProperty, value);
            }
        }

        public static DependencyProperty ContentsProperty = DependencyProperty.Register("Contents",
            typeof(string), typeof(HttpDataReader));

        [RequiredInputParam]
        [Name("Contents")]
        [Description("Data as string read from the HTTP location.")]
        public string Contents
        {
            get
            {
                return ((string)(base.GetValue(HttpDataReader.ContentsProperty)));
            }
            set
            {
                base.SetValue(HttpDataReader.ContentsProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            // used to build entire input
            StringBuilder sb = new StringBuilder();

            // used on each read operation
            byte[] buf = new byte[8192];

            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)
                WebRequest.Create(this.FileName.OriginalString);

            // execute the request
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();

            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int count = 0;

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.ASCII.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?

            // print out page source
            this.Contents = sb.ToString();

            return ActivityExecutionStatus.Closed;
        }
    }
}
